package com.hb.trade.server.flow.controller;

import cn.hutool.core.util.StrUtil;
import com.hb.core.pojo.flow.entity.FlowTempMain;
import com.hb.core.pojo.flow.dto.FlowTempMainDTO;
import com.hb.core.pojo.parent.MPBaseController;
import com.hb.trade.server.flow.manager.FlowTempMainManager;
import com.hb.trade.server.flow.service.FlowTempMainService;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.model.enums.MsgEnum;
import org.fzzn.core.util.MsgUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 流程主表(模板) 前端控制器
 *
 * @author zhaojk
 * @since 2021-05-27
 */
@RestController
@RequestMapping("/flowTempMain")
public class FlowTempMainController extends MPBaseController<FlowTempMain, FlowTempMainService> {

    public FlowTempMainController(FlowTempMainService service) {
        super(service);
    }

    /**
     * 定义一个流程
     *
     * @return
     */
    @PostMapping("/define")
    public Message define(@RequestBody FlowTempMainDTO flowTempMainDto) {
        // 参数验证
        String errorMsg = flowTempMainDto.to(FlowTempMainManager.class).paramsValid();

        if (StrUtil.isNotEmpty(errorMsg)) { // 无验证消息返回时表示验证通过
            return MsgUtil.fail(errorMsg);
        }
        String msg = service.defineFlowTemp(flowTempMainDto);
        if (StrUtil.isEmpty(msg)) {
            return MsgUtil.success();
        }
        return MsgUtil.fail(msg);
    }
    /**
     * 定义一个流程
     *
     * @return
     */
    @PostMapping("/detail")
    public Message<FlowTempMainDTO> detail(@RequestBody FlowTempMainDTO flowTempMainDto) {
        Long tempMainId = flowTempMainDto.getId();
        if (tempMainId == null) {
            return MsgUtil.msg(MsgEnum.NULL_RESULT);
        }
        FlowTempMainDTO orgFlowTemp = service.getAllById(tempMainId);
        return MsgUtil.success(orgFlowTemp);
    }
}
